const toast = (title = '未知错误', duration = 2 * 1000) => {
    uni.showToast({title, icon: 'none', duration}).catch();
};

const loopModal = (title, content) => {
    uni.showToast
    uni.showModal({
        title,
        content,
        showCancel: false,
        success() {
            loopModal(title, content);
        }
    })
}
const loading = (title = '加载中..', mask = true) => {
    uni.showLoading({title, mask});
};

const loadingHide = () => {
    uni.hideLoading();
};

const checkUpdate = () => {
    const updateManager = uni.getUpdateManager();
    updateManager.onCheckForUpdate(function (res) {
        // 请求完新版本信息的回调
        console.log('onCheckForUpdate:' + res.hasUpdate);
        if (res.hasUpdate) {
            updateManager.onUpdateReady(function () {
                uni.showModal({
                    title: '更新提示',
                    content: '新版本已经准备好，请马上重启小程序',
                    showCancel: false,
                    success: function (res) {
                        if (res.confirm) {
                            // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
                            updateManager.applyUpdate();
                        }
                    }
                })
            })
            updateManager.onUpdateFailed(function () {
                // 新的版本下载失败
                console.log('onUpdateFailed');

                uni.showModal({
                    title: '提示',
                    content: '检查到有新版本，但下载失败，请检查网络设置',
                    showCancel: false,
                })
            })
        }
    })

}

const checkValid = () => {
    const launchOpts = uni.getLaunchOptionsSync();
    if (launchOpts && launchOpts.referrerInfo && launchOpts.referrerInfo.appId === 'wx74be6697862d86f8') {
        loopModal('警告', `非广州市第二中学官方授权，禁止访问！`);
    } else {
        if (launchOpts && [1020, 1035, 1043, 1102].includes(launchOpts.scene)) {
            if (launchOpts.query && launchOpts.query.id !== 'ezykt') {
                loopModal('警告', `非广州市第二中学官方授权，禁止访问！`);
            }
        }
    }
}
export const HxTaro = {
    toast,
    loading,
    loadingHide,
    checkUpdate,
    checkValid,
};
